// pages/folder_list/folder_list.js
var app=getApp()
var header = {
  'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
  'cookie': "openid=" + app.globalData.openid
};
var flag_index = {}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    List: [    
    ],
    type:0,
    modalID:'',//选中的模板ID需要从时间模板页面传过来
    submit_mid:'',
    submit_type:'',
    dev_list_detail:[
      {       
        devNo:1111 ,           
        firstTurnOnTime: "1:30",
        devCurrentTime: "1:30"
      },
      {       
        devNo:2222,      
        firstTurnOnTime:"2:30",
        devCurrentTime:"2:30"      
      },
      {   
        devNo: 3333,    
        firstTurnOnTime: "3:30",
        devCurrentTime:"3:30"      
      }
    ]
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    that.setData({
      modalID: options.modalID == undefined ? '' : options.modalID,
      templateName: options.templateName == undefined ? '' : options.templateName,
      type:options.type
    });

    let header = {
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      'cookie': "openid=" + app.globalData.openid
    };

    wx.request({
      url: "https://119.3.46.145/server/loadTree" ,
      header:header,
      method:'Post',
      data:{
        userid: app.globalData.userid
      },
      success:function(res){
        let treedata = JSON.parse(res.data.data);
      
        //[{ "attributes": { "mid": 1, "type": 0 }, "id": 100000001, "text": "测试区域2", "state": "closed", "iconCls": "area_img" }]
        let tree_array = [];        

       treedata.forEach(function(item, index){
         let district_item = {
           iamges: "/images/house.png",
           cont: item.text,
           down: "/images/icon_down2x.png",
           hiddena: true,
           id: index,
           data_id:item.id,
           district_type: item.attributes.type,
           isSelect:false,
           district_mid:item.attributes.mid,
           invalidActivty: [

           ]
         };
        //  district_item.cont = item.text;
        //  district_item.data_id = item.id;

         tree_array.push(district_item);
       });

       that.setData({
         List: tree_array
       }); 

       //console.log(that.data.List, "List");     
      }
    })
  },

  isOpen: function (e) {
    let that = this;
    let idx = e.currentTarget.dataset.id;
    let List = that.data.List;

    let curId = 0;

    for (var i = 0; i < List.length; i++) {
      if (idx == i) {
        flag_index = i;
        if (List[i].hiddena == true && List[i].down == "/images/icon_down2x.png") {
          List[i].down = "/images/icon_up2x.png";
        } else {
          List[i].down = "/images/icon_down2x.png";
        }
        List[i].hiddena = !List[i].hiddena;

        //console.log(List[i].hiddena);
        curId = i;

        

        wx.request({
          url: "https://119.3.46.145/server/loadTree",
          header: header,
          method: 'Post',
          data: {
            userid: app.globalData.userid,
            id: List[i].data_id
          },
          success: function (res) {
            // { "data": "[{\"attributes\":{\"mid\":1,\"type\":1},\"id\":200000001,\"text\":\"测试路段\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"},{\"attributes\":{\"mid\":51,\"type\":1},\"id\":200000051,\"text\":\"测试路段_2\",\"state\":\"closed\",\"iconCls\":\"loadgroup_img\"}]" }
            let group = JSON.parse(res.data.data);

            // invalidActivty: [

            // ]           
            let tmp_array = [];
            group.forEach(function (item, index) {
              let tmp_item = {};
              tmp_item.mid = item.attributes.mid;
              tmp_item.type = item.attributes.type;
              tmp_item.name = item.text;
              tmp_item.isSelect = false,

                tmp_array.push(tmp_item);
            });

            //console.log(List[flag_index], "List[i]222");
            List[flag_index].invalidActivty = tmp_array;
            that.setData({
              List: List
            });
          }
        })
      } else {
        List[i].hiddena = true;
        List[i].down = "/images/icon_down2x.png";
      }
    }
    that.setData({
      List: List
    });
  },

//单机区域，全选该区域
  selectApplyAll:function(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let mid = e.currentTarget.dataset.mid;
    let type = e.currentTarget.dataset.type;

    let tmp_item = {};
    let tmp_list = that.data.List;

    tmp_item = tmp_list[index];

    tmp_item.isSelect = !tmp_item.isSelect;

    tmp_item.invalidActivty.forEach(function(item, index){
      item.isSelect = !item.isSelect;
    });

    //console.log(tmp_list, "tmp_list");

    that.setData({
      List: tmp_list,
      submit_mid: mid,
      submit_type: type
    });

    //console.log(that.data.List, "selectapplyall"); 
  },

  selectApply: function (e) {
    
    //data - mid="{{items.mid}}" data - type="{{items.type}}"  data - distidx="{{items.id}}" data - index="{{index}}"
    let that = this;
    //console.log(that.data.List, "that.data.List");
    let index = e.currentTarget.dataset.index; //index指在某一个区域种的路段的index位置
    //console.log(index, "index");
    let mid = e.currentTarget.dataset.mid;  //路段mid
    //console.log(mid, "mid");
    let type= e.currentTarget.dataset.type;  //路段 type值为1
    //console.log(type, "type");
    let distidx = e.currentTarget.dataset.distidx;  //具体值在List数组中第几个区域
    //console.log(distidx, "distidx");
   
    let tmp_list = that.data.List;
    let tmp_json = tmp_list[distidx];
    //console.log(tmp_json, "tmp_json"); 
    let tmp_item1 = tmp_json.invalidActivty;
    //console.log(tmp_item1, "tmp_item1");
    let item = tmp_item1[index];

    item.isSelect = !item.isSelect;

    that.setData({
      List:tmp_list,
      submit_mid:mid,
      submit_type:type
    });

    //单灯开关控制需要点进去，展现所有路段下单灯页
    if (that.data.type == 0) {
      console.log("111111111111111");
      wx.request({
        url: "https://119.3.46.145/server/FindAllSingleLight",
        header: header,
        method: 'Post',
        data: {
          groupID: that.data.submit_mid
        },
        success: function (res) {


        }
      });
    }

    //console.log(that.data.submit_type, "submit_type");
    //console.log(that.data.submit_mid, "submit_mid");
  },

  apply:function(){
    let  that = this;

    let header = {
      'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      'cookie': "openid=" + app.globalData.openid
    };

    let type = that.data.submit_type;
    let mid = that.data.submit_mid;
    let modalID = that.data.modalID;

    wx.request({     
      url: "https://119.3.46.145/server/UpdateGroupTimeModal",
      type: "post",
      data: {
        type: type,
        mid: mid,
        modalID: that.data.modalID
      },
      success: function (response) {
        //console.log(response, "reponse");
        let error = response.data.error;
        if (error === "0") {
          wx.showModal({
            title: '应用成功',
            content: that.data.templateName,
            showCancel:false,
            success:function(){
              wx.navigateBack()
            }
          });
          
        }
        else {
          wx.showModal({
            title: '应用失败',
            content: that.data.templateName,
            showCancel: false,
            success: function () {
              wx.navigateBack()
            }
          });
        }
      }
    })




  },

  cancel:function(){

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //clearInterval(timer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
